{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1.6 矩阵"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.创建矩阵"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.401621Z",
     "end_time": "2024-05-07T20:11:04.889038Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1, 2, 3]])"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.mat([1, 2, 3])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "(1, 3)"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.889038Z",
     "end_time": "2024-05-07T20:11:04.904960Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.908205Z",
     "end_time": "2024-05-07T20:11:04.966058Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "(3,)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.927744Z",
     "end_time": "2024-05-07T20:11:04.966058Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1, 2, 3],\n        [4, 5, 6],\n        [7, 8, 9]])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = np.mat('1 2 3; 4 5 6; 7 8 9')\n",
    "B"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.949422Z",
     "end_time": "2024-05-07T20:11:04.966058Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1, 2, 3]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = np.mat(a)\n",
    "C"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.966058Z",
     "end_time": "2024-05-07T20:11:04.982484Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1., 0., 2., 0.],\n        [0., 1., 0., 2.],\n        [2., 0., 1., 0.],\n        [0., 2., 0., 1.]])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M1 = np.mat(np.eye(2))\n",
    "M2 = 2 * M1\n",
    "D = np.bmat('M1 M2; M2 M1')\n",
    "D"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:04.987044Z",
     "end_time": "2024-05-07T20:11:05.043518Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.乘法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 0, 3],\n       [0, 5, 6],\n       [7, 8, 0]])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1, 10).reshape((3, 3))\n",
    "b = np.array([[1, 0, 1], [0, 1, 1], [1, 1, 0]])\n",
    "a * b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.005958Z",
     "end_time": "2024-05-07T20:11:05.047666Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[ 4,  5,  3],\n        [10, 11,  9],\n        [16, 17, 15]])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.mat(a)\n",
    "B = np.mat(b)\n",
    "A * B"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.026299Z",
     "end_time": "2024-05-07T20:11:05.047666Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 4,  5,  3],\n       [10, 11,  9],\n       [16, 17, 15]])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.047666Z",
     "end_time": "2024-05-07T20:11:05.064418Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.基本操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[3, 0, 1],\n        [0, 2, 2],\n        [1, 2, 4]])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.mat('3 0 1; 0 2 2; 1 2 4')\n",
    "A"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.067229Z",
     "end_time": "2024-05-07T20:11:05.127906Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[3, 0, 1],\n        [0, 2, 2],\n        [1, 2, 4]])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.085178Z",
     "end_time": "2024-05-07T20:11:05.127906Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[ 0.4,  0.2, -0.2],\n        [ 0.2,  1.1, -0.6],\n        [-0.2, -0.6,  0.6]])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.I"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.103651Z",
     "end_time": "2024-05-07T20:11:05.127906Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1., 0., 0.],\n        [0., 1., 0.],\n        [0., 0., 1.]])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A * A.I"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.120896Z",
     "end_time": "2024-05-07T20:11:05.145355Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1., 0., 0., 0.],\n        [0., 1., 0., 0.],\n        [0., 0., 1., 0.],\n        [0., 0., 0., 1.]])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mat(np.eye(4))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.145355Z",
     "end_time": "2024-05-07T20:11:05.228115Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.斐波那契数列"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[1, 1],\n        [1, 0]])"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = np.mat([[1, 1], [1, 0]])\n",
    "F"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.166535Z",
     "end_time": "2024-05-07T20:11:05.228348Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "matrix([[21, 13],\n        [13,  8]])"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f8 = F ** 7\n",
    "f8"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.189014Z",
     "end_time": "2024-05-07T20:11:05.228348Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "21"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f8[0, 0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.211525Z",
     "end_time": "2024-05-07T20:11:05.228348Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:11:05.228348Z",
     "end_time": "2024-05-07T20:11:05.244637Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
